#!/bin/bash
# Copyright 2017-2019 ControlScan, Inc.
#
# This file is part of Cyphon Engine.
#
# Cyphon Engine is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Cyphon Engine is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Cyphon Engine. If not, see <http://www.gnu.org/licenses/>.

### BEGIN INIT INFO
# Provides:          receiver
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Provides Cyphon queue consumers for Logstash
# Description:       Provides Cyphon queue consumers for Logstash
### END INIT INFO

# Source function library.
. /etc/init.d/functions

PATH=/sbin:/usr/sbin:/bin:/usr/bin

HOME=/opt/cyphon
VIRTUAL_ENV=$HOME/venv
PYTHON=$VIRTUAL_ENV/bin/python

PROGRAM_NAME="receiver"
PIDFILE=/var/run/$PROGRAM_NAME.pid
LOCKFILE=/var/lock/subsys/$PROGRAM_NAME

PROGRAM_DIR=$HOME/web/django
PROGRAM=$PROGRAM_DIR/$PROGRAM_NAME.py

CONSUMERS=4
ARGS=$CONSUMERS

USER=root

start() {
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        PARENT_ID=parent_id
        if [ -z "`pgrep ${PID%% *}`" ] && [ "${PID%% *}" != find_parent_pid ]; then
            printf "%s\n" "Process dead but pidfile exists"
        else
            printf "$PROGRAM_NAME is already running!\n"
        fi
    else
        printf "%-50s" "Starting $PROGRAM_NAME ..."
        source $VIRTUAL_ENV/bin/activate
        cd $PROGRAM_DIR
        umask 002
        daemon --user $USER $PYTHON $PROGRAM $ARGS &
        sleep 5
        PID=`ps aux|grep -vE 'grep|runuser|bash'|grep -w "$PROGRAM"|awk '{print $2}'`
        if [ -z "$PID" ]; then
            printf "[ \e[31mFAIL\033[0m ]\n"
        else
            echo $PID > $PIDFILE
            printf "[ \e[32mOK\033[0m ]\n"
        fi
    fi
}

stop() {
    printf "%-50s" "Shutting down $PROGRAM_NAME:"
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        kill -HUP $PID 2>/dev/null
        printf "[ \e[32mOK\033[0m ]\n"
        rm -f $PIDFILE
    else
        printf "[ \e[31mFAIL\033[0m ]\n" 
    fi
}

check_status() {
    printf "%-50s" "Checking $PROGRAM_NAME ..."
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -z "`pgrep ${PID%% *}`" ] && [ "${PID%% *}" != "`ps aux|grep -vE 'grep|runuser|bash'|grep -w "$PROGRAM"|awk 'NR==1'|awk '{print $2}'`" ]; then
            printf "%s\n" "Process dead but pidfile exists"
        else
            printf "[ \e[32mRUNNING\033[0m ]\n"
        fi
    else
        printf "[ \e[31mSTOPPED\033[0m ]\n"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        check_status
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: $PROGRAM_NAME {start|stop|status|restart}"
        exit 1
        ;;
esac
exit 1
